<!doctype html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  
    
      
    

    
  

  

  
    
      
    

    
  

  
    
      
    

    
  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Monda:300,300italic,400,400italic,700,700italic|Roboto Slab:300,300italic,400,400italic,700,700italic|Lobster Two:300,300italic,400,400italic,700,700italic|PT Mono:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Laravel,PHPUnit," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.0" />






<meta name="description" content="最近在学习使用PHPUnit写测试，在给laravel项目做测试时，因为项目已经有一个数据库了，并且其中也有不少数据，为了防止测试数据污染现有数据。于是想到针对测试可以建立一个相同的数据库，这样测试和原数据库将分离互补干扰。下面就将说说laravel框架如何连接多个数据库。
首先，在数据库配置文件app/config/database.php中创建新数据库这里我新建的数据库为mysql_testi">
<meta property="og:type" content="article">
<meta property="og:title" content="Laravel如何连接多个数据库？">
<meta property="og:url" content="http://yoursite.com/2017/05/23/Laravel如何连接多个数据库？/index.html">
<meta property="og:site_name" content="EasyBlog">
<meta property="og:description" content="最近在学习使用PHPUnit写测试，在给laravel项目做测试时，因为项目已经有一个数据库了，并且其中也有不少数据，为了防止测试数据污染现有数据。于是想到针对测试可以建立一个相同的数据库，这样测试和原数据库将分离互补干扰。下面就将说说laravel框架如何连接多个数据库。
首先，在数据库配置文件app/config/database.php中创建新数据库这里我新建的数据库为mysql_testi">
<meta property="og:updated_time" content="2017-05-23T07:11:55.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Laravel如何连接多个数据库？">
<meta name="twitter:description" content="最近在学习使用PHPUnit写测试，在给laravel项目做测试时，因为项目已经有一个数据库了，并且其中也有不少数据，为了防止测试数据污染现有数据。于是想到针对测试可以建立一个相同的数据库，这样测试和原数据库将分离互补干扰。下面就将说说laravel框架如何连接多个数据库。
首先，在数据库配置文件app/config/database.php中创建新数据库这里我新建的数据库为mysql_testi">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2017/05/23/Laravel如何连接多个数据库？/"/>





  <title> Laravel如何连接多个数据库？ | EasyBlog </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?07a6d49ae67fe991e0ce3c2f3955e0f9";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>










  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">EasyBlog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <h1 class="site-subtitle" itemprop="description"></h1>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/05/23/Laravel如何连接多个数据库？/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Gasbylei">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="http://gasbylei.github.io/uploads/avatar.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="EasyBlog">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
            
            
              
                Laravel如何连接多个数据库？
              
            
          </h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-05-23T14:13:05+08:00">
                2017-05-23
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2017/05/23/Laravel如何连接多个数据库？/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count" data-disqus-identifier="2017/05/23/Laravel如何连接多个数据库？/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>最近在学习使用PHPUnit写测试，在给laravel项目做测试时，因为项目已经有一个数据库了，并且其中也有不少数据，为了防止测试数据污染现有数据。于是想到针对测试可以建立一个相同的数据库，这样测试和原数据库将分离互补干扰。下面就将说说laravel框架如何连接多个数据库。</p>
<h4 id="首先，在数据库配置文件app-config-database-php中创建新数据库"><a href="#首先，在数据库配置文件app-config-database-php中创建新数据库" class="headerlink" title="首先，在数据库配置文件app/config/database.php中创建新数据库"></a>首先，在数据库配置文件<code>app/config/database.php</code>中创建新数据库</h4><p>这里我新建的数据库为<code>mysql_testing</code></p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">return</span> [</div><div class="line"></div><div class="line"> <span class="string">'default'</span> =&gt; env(<span class="string">'DB_CONNECTION'</span>, <span class="string">'mysql'</span>),</div><div class="line"> </div><div class="line"> <span class="string">'connections'</span> =&gt; [</div><div class="line"> </div><div class="line">    <span class="string">'mysql'</span> =&gt; [</div><div class="line">            <span class="string">'driver'</span> =&gt; <span class="string">'mysql'</span>,</div><div class="line">            <span class="string">'host'</span> =&gt; env(<span class="string">'DB_HOST'</span>, <span class="string">'127.0.0.1'</span>),</div><div class="line">            <span class="string">'port'</span> =&gt; env(<span class="string">'DB_PORT'</span>, <span class="string">'3306'</span>),</div><div class="line">            <span class="string">'database'</span> =&gt; env(<span class="string">'DB_DATABASE'</span>, <span class="string">'forge'</span>),</div><div class="line">            <span class="string">'username'</span> =&gt; env(<span class="string">'DB_USERNAME'</span>, <span class="string">'forge'</span>),</div><div class="line">            <span class="string">'password'</span> =&gt; env(<span class="string">'DB_PASSWORD'</span>, <span class="string">''</span>),</div><div class="line">            <span class="string">'unix_socket'</span> =&gt; env(<span class="string">'DB_SOCKET'</span>, <span class="string">''</span>),</div><div class="line">            <span class="string">'charset'</span> =&gt; <span class="string">'utf8mb4'</span>,</div><div class="line">            <span class="string">'collation'</span> =&gt; <span class="string">'utf8mb4_unicode_ci'</span>,</div><div class="line">            <span class="string">'prefix'</span> =&gt; <span class="string">''</span>,</div><div class="line">            <span class="string">'strict'</span> =&gt; <span class="keyword">true</span>,</div><div class="line">            <span class="string">'engine'</span> =&gt; <span class="keyword">null</span>,</div><div class="line">        ],</div><div class="line"></div><div class="line">    <span class="string">'mysql_testing'</span> =&gt; [</div><div class="line">            <span class="string">'driver'</span> =&gt; <span class="string">'mysql'</span>,</div><div class="line">            <span class="string">'host'</span> =&gt; env(<span class="string">'DB_HOST_TEST'</span>, <span class="string">'127.0.0.1'</span>),</div><div class="line">            <span class="string">'port'</span> =&gt; env(<span class="string">'DB_PORT_TEST'</span>, <span class="string">'3306'</span>),</div><div class="line">            <span class="string">'database'</span> =&gt; env(<span class="string">'DB_DATABASE_TEST'</span>, <span class="string">'forge'</span>),</div><div class="line">            <span class="string">'username'</span> =&gt; env(<span class="string">'DB_USERNAME_TEST'</span>, <span class="string">'forge'</span>),</div><div class="line">            <span class="string">'password'</span> =&gt; env(<span class="string">'DB_PASSWORD_TEST'</span>, <span class="string">''</span>),</div><div class="line">            <span class="string">'unix_socket'</span> =&gt; env(<span class="string">'DB_SOCKET'</span>, <span class="string">''</span>),</div><div class="line">            <span class="string">'charset'</span> =&gt; <span class="string">'utf8mb4'</span>,</div><div class="line">            <span class="string">'collation'</span> =&gt; <span class="string">'utf8mb4_unicode_ci'</span>,</div><div class="line">            <span class="string">'prefix'</span> =&gt; <span class="string">''</span>,</div><div class="line">            <span class="string">'strict'</span> =&gt; <span class="keyword">true</span>,</div><div class="line">            <span class="string">'engine'</span> =&gt; <span class="keyword">null</span>,</div><div class="line">        ],</div><div class="line">     ],</div><div class="line"> ];</div></pre></td></tr></table></figure>
<a id="more"></a>
<h4 id="为了连接新数据库，因此在-env文件中添加新数据库的配置信息"><a href="#为了连接新数据库，因此在-env文件中添加新数据库的配置信息" class="headerlink" title="为了连接新数据库，因此在.env文件中添加新数据库的配置信息"></a>为了连接新数据库，因此在<code>.env</code>文件中添加新数据库的配置信息</h4><figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">DB_CONNECTION=mysql</div><div class="line">DB_HOST=<span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span></div><div class="line">DB_PORT=<span class="number">3306</span></div><div class="line">DB_DATABASE=database1</div><div class="line">DB_USERNAME=root</div><div class="line">DB_PASSWORD=</div><div class="line"></div><div class="line">DB_HOST_TEST=<span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span></div><div class="line">DB_PORT_TEST=<span class="number">3306</span></div><div class="line">DB_DATABASE_TEST=database2</div><div class="line">DB_USERNAME_TEST=root</div><div class="line">DB_PASSWORD_TEST=</div></pre></td></tr></table></figure>
<p>这样数据库就连接成功。</p>
<h4 id="如何使用第二个数据库？"><a href="#如何使用第二个数据库？" class="headerlink" title="如何使用第二个数据库？"></a>如何使用第二个数据库？</h4><p>在Schema中，可以指定其使用的连接，可以使用<code>connection()</code>方法：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">Schema::connection(<span class="string">'mysql2'</span>)-&gt;create(<span class="string">'some_table'</span>, <span class="function"><span class="keyword">function</span><span class="params">($table)</span></span></div><div class="line">&#123;</div><div class="line">    $table-&gt;increments(<span class="string">'id'</span>):</div><div class="line">&#125;);</div></pre></td></tr></table></figure>
<p>当使用多个数据连接时，你可以使用 <code>DB</code> facade 的 <code>connection</code> 方法。在 <code>config/database.php</code>中定义好的数据库连接 <code>name</code> 作为 <code>connection</code> 的参数进行传递。</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$users = DB::connection(<span class="string">'mysql2'</span>)-&gt;select(...);</div></pre></td></tr></table></figure>
<p>在Eloquent Model中，可以指定其使用什么数据库，只需要定义一个<code>$connection</code>变量。</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&lt;?php</span></div><div class="line"></div><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">SomeModel</span> <span class="keyword">extends</span> <span class="title">Eloquent</span> </span>&#123;</div><div class="line"></div><div class="line">    <span class="keyword">protected</span> $connection = <span class="string">'mysql2'</span>;</div><div class="line"></div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>还可以通过<code>setConnection</code>方法在controller中设置要连接相应的数据库。</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&lt;?php</span></div><div class="line"></div><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">SomeController</span> <span class="keyword">extends</span> <span class="title">BaseController</span> </span>&#123;</div><div class="line"></div><div class="line">    <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">someMethod</span><span class="params">()</span></span></div><div class="line">    &#123;</div><div class="line">        $someModel = <span class="keyword">new</span> SomeModel;</div><div class="line"></div><div class="line">        $someModel-&gt;setConnection(<span class="string">'mysql2'</span>);</div><div class="line"></div><div class="line">        $something = $someModel-&gt;find(<span class="number">1</span>);</div><div class="line"></div><div class="line">        <span class="keyword">return</span> $something;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>那么在连接多个数据库后，如何在其他数据库中生成数据表呢？</p>
<p>可以使用下面的命令，即可给<code>mysql2</code>数据库生成数据表：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">php artisan migrate --database=mysql2</div></pre></td></tr></table></figure>
<p>####在PHPUnit中指定要测试的数据库<br>在<code>phpunit.xml</code>文件中加入如下：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&lt;env name=<span class="string">"DB_CONNECTION"</span> value=<span class="string">"mysql_testing"</span>/&gt;</div></pre></td></tr></table></figure>
<p><code>value</code>是指定的数据库名，这个数据库名是在<code>app/config/database.php</code>中定义过的。</p>
<h5 id="在测试后，重置数据库有两种方法"><a href="#在测试后，重置数据库有两种方法" class="headerlink" title="在测试后，重置数据库有两种方法"></a>在测试后，重置数据库有两种方法</h5><ol>
<li><p>使用迁移：<br>其中有一种方式就是在每次测试后都还原数据库，并在下次测试前运行迁移。Laravel 提供了简洁的 <code>DatabaseMigrations</code> trait，它会自动帮你处理好这些操作。你只需在测试类中使用此 trait 即可：</p>
 <figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&lt;?php</span></div><div class="line"></div><div class="line"><span class="keyword">namespace</span> <span class="title">Tests</span>\<span class="title">Feature</span>;</div><div class="line"></div><div class="line"><span class="keyword">use</span> <span class="title">Tests</span>\<span class="title">TestCase</span>;</div><div class="line"><span class="keyword">use</span> <span class="title">Illuminate</span>\<span class="title">Foundation</span>\<span class="title">Testing</span>\<span class="title">WithoutMiddleware</span>;</div><div class="line"><span class="keyword">use</span> <span class="title">Illuminate</span>\<span class="title">Foundation</span>\<span class="title">Testing</span>\<span class="title">DatabaseMigrations</span>;</div><div class="line"><span class="keyword">use</span> <span class="title">Illuminate</span>\<span class="title">Foundation</span>\<span class="title">Testing</span>\<span class="title">DatabaseTransactions</span>;</div><div class="line"></div><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">ExampleTest</span> <span class="keyword">extends</span> <span class="title">TestCase</span></span></div><div class="line">&#123;</div><div class="line">    <span class="keyword">use</span> <span class="title">DatabaseMigrations</span>;</div><div class="line"></div><div class="line">    <span class="comment">/**</span></div><div class="line">     * 基本的功能测试示例。</div><div class="line">     *</div><div class="line">     * <span class="doctag">@return</span> void</div><div class="line">     */</div><div class="line">    <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">testBasicExample</span><span class="params">()</span></span></div><div class="line">    &#123;</div><div class="line">        $response = <span class="keyword">$this</span>-&gt;get(<span class="string">'/'</span>);</div><div class="line"></div><div class="line">        <span class="comment">// ...</span></div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
</li>
<li><p>使用事务：<br>另一个方式，就是将每个测试案例都包含在数据库事务中。Laravel 提供了一个简洁的 <code>DatabaseTransactions</code> trait 来自动帮你处理好这些操作。</p>
 <figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&lt;?php</span></div><div class="line"></div><div class="line"><span class="keyword">namespace</span> <span class="title">Tests</span>\<span class="title">Feature</span>;</div><div class="line"></div><div class="line"><span class="keyword">use</span> <span class="title">Tests</span>\<span class="title">TestCase</span>;</div><div class="line"><span class="keyword">use</span> <span class="title">Illuminate</span>\<span class="title">Foundation</span>\<span class="title">Testing</span>\<span class="title">WithoutMiddleware</span>;</div><div class="line"><span class="keyword">use</span> <span class="title">Illuminate</span>\<span class="title">Foundation</span>\<span class="title">Testing</span>\<span class="title">DatabaseMigrations</span>;</div><div class="line"><span class="keyword">use</span> <span class="title">Illuminate</span>\<span class="title">Foundation</span>\<span class="title">Testing</span>\<span class="title">DatabaseTransactions</span>;</div><div class="line"></div><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">ExampleTest</span> <span class="keyword">extends</span> <span class="title">TestCase</span></span></div><div class="line">&#123;</div><div class="line">    <span class="keyword">use</span> <span class="title">DatabaseTransactions</span>;</div><div class="line"></div><div class="line">    <span class="comment">/**</span></div><div class="line">     * 基本的功能测试示例。</div><div class="line">     *</div><div class="line">     * <span class="doctag">@return</span> void</div><div class="line">     */</div><div class="line">    <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">testBasicExample</span><span class="params">()</span></span></div><div class="line">    &#123;</div><div class="line">        $response = <span class="keyword">$this</span>-&gt;get(<span class="string">'/'</span>);</div><div class="line"></div><div class="line">        <span class="comment">// ...</span></div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
</li>
</ol>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>


    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Laravel/" rel="tag"># Laravel</a>
          
            <a href="/tags/PHPUnit/" rel="tag"># PHPUnit</a>
          
        </div>
      

      
        
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/05/17/PHP流程控制/" rel="next" title="PHP流程控制">
                <i class="fa fa-chevron-left"></i> PHP流程控制
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/06/05/Laravel-Collection常用方法/" rel="prev" title="Laravel Collection常用方法">
                Laravel Collection常用方法 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>

          
          </div>
          


          
  <div class="comments" id="comments">
    
      <div id="disqus_thread">
        <noscript>
          Please enable JavaScript to view the
          <a href="//disqus.com/?ref_noscript">comments powered by Disqus.</a>
        </noscript>
      </div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="http://gasbylei.github.io/uploads/avatar.png"
               alt="Gasbylei" />
          <p class="site-author-name" itemprop="name">Gasbylei</p>
           
              <p class="site-description motion-element" itemprop="description"></p>
           
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/archives">
                <span class="site-state-item-count">17</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">6</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/Gasbylei" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-4"><a class="nav-link" href="#首先，在数据库配置文件app-config-database-php中创建新数据库"><span class="nav-number">1.</span> <span class="nav-text">首先，在数据库配置文件app/config/database.php中创建新数据库</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#为了连接新数据库，因此在-env文件中添加新数据库的配置信息"><span class="nav-number">2.</span> <span class="nav-text">为了连接新数据库，因此在.env文件中添加新数据库的配置信息</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#如何使用第二个数据库？"><span class="nav-number">3.</span> <span class="nav-text">如何使用第二个数据库？</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#在测试后，重置数据库有两种方法"><span class="nav-number">3.1.</span> <span class="nav-text">在测试后，重置数据库有两种方法</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy;  2015 - 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Gasbylei</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Pisces
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    
    
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  



  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>

  
  <script type="text/javascript" src="/lib/canvas-nest/canvas-nest.min.js"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.0"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.0"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.0"></script>



  



  

    <script type="text/javascript">
      var disqus_shortname = 'gatsbylei';
      var disqus_identifier = '2017/05/23/Laravel如何连接多个数据库？/';

      var disqus_title = "Laravel如何连接多个数据库？";


      function run_disqus_script(disqus_script) {
        var dsq = document.createElement('script');
        dsq.type = 'text/javascript';
        dsq.async = true;
        dsq.src = '//' + disqus_shortname + '.disqus.com/' + disqus_script;
        (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
      }

      run_disqus_script('count.js');

      
        var disqus_config = function () {
            this.page.url = disqus_url;
            this.page.identifier = disqus_identifier;
            this.page.title = disqus_title;
        };
        run_disqus_script('embed.js');
      

    </script>
  










  
  

  

  

  

  


  

</body>
</html>
